package com.jht.bletool2java.activity.homepage;


import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.github.rahatarmanahmed.cpv.CircularProgressView;
import com.google.android.material.tabs.TabLayout;
import com.jht.bletool2java.BLEApplication;
import com.jht.bletool2java.R;
import com.jht.bletool2java.activity.BaseActivity;
import com.jht.bletool2java.activity.homepage.adapter.HomeViewPageAdapter;
import com.jht.bletool2java.activity.homepage.fragment.bledevice.BleDevicesFragment;
import com.jht.bletool2java.activity.homepage.fragment.simulationble.SimulationBleFragment;
import com.jht.bletool2java.activity.setting.SettingActivity;

import java.util.ArrayList;
import java.util.List;

public class HomeActivity extends BaseActivity implements IHome.UI,View.OnClickListener, BleDevicesFragment.ScanUICallback {
    private static final String TAG = "HomeActivity";

    private RelativeLayout mCommonParent;
    private ImageView mIvBack;
    private ImageView mIvMenu;
    private TextView mTvTitle;
    private TextView mTvTitle2;
    private CircularProgressView mProgressView;
    private ImageView mIvEdit;
    private TabLayout mTabLayout;
    private ViewPager mVpMain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_home;
    }

    @Override
    public void initViewAndPresenter() {
        mIvMenu = findViewById(R.id.iv_menu);
        mProgressView = findViewById(R.id.progress_view);
        mCommonParent = findViewById(R.id.common_parent);
        mIvBack = findViewById(R.id.iv_back);
        mTvTitle = findViewById(R.id.tv_title);
        mTvTitle2 = findViewById(R.id.tv_title2);
        mIvEdit = findViewById(R.id.iv_edit);
        mTabLayout = findViewById(R.id.tab_layout);
        mVpMain = findViewById(R.id.vp_main);

        mIvMenu.setOnClickListener(this);
        mProgressView.setVisibility(View.INVISIBLE);
        initViewPage();
        mTabLayout.setupWithViewPager(mVpMain);
    }

    private void initViewPage() {
        List<Fragment> fragments = initFragments();
        HomeViewPageAdapter myViewPageAdapter = new HomeViewPageAdapter(getSupportFragmentManager(), fragments,initFragmentHeader());
        mVpMain.setAdapter(myViewPageAdapter);
    }

    private String[] initFragmentHeader() {
        String[] header_str = new String[2];
        Resources resources = BLEApplication.getBLEApplicationContext().getResources();
        header_str[0] = resources.getString(R.string.find_device);
        header_str[1] = resources.getString(R.string.simulation_device);
        return header_str;
    }

    private List<Fragment> initFragments() {
        BleDevicesFragment bleDevicesFragment = BleDevicesFragment.newInstance();
        bleDevicesFragment.setScanUICallback(this);
        SimulationBleFragment simulationBleFragment = SimulationBleFragment.newInstance();
        List<Fragment> data = new ArrayList<>();
        data.add(bleDevicesFragment);
        data.add(simulationBleFragment);
        return data;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_menu:
                startSettingActivity();
                break;
        }
    }

    private void startSettingActivity(){
        Intent intent = new Intent(this, SettingActivity.class);
        startActivity(intent);
    }

    @Override
    public void stopBtScanUI() {
        mProgressView.setVisibility(View.GONE);
    }

    @Override
    public void startBtScanUI(){
        mProgressView.setVisibility(View.VISIBLE);
    }
}